import codecs
import hashlib
import os
import re
import config_setting


def getUrl_from_content(content_head):
    pattern = re.compile('url: (.*?)\s')
    url_list = pattern.findall(content_head)
    return url_list[0] if len(url_list) > 0 else ""


def getHash_from_path(path):
    pattern = re.compile('__(.*?)\\.html')
    hash_list = pattern.findall(path)
    return hash_list[0] if len(hash_list) > 0 else ""


def hash_url(url):
    return hashlib.sha1(url.encode("utf-8")).hexdigest()


def truncate_text(text):
    # 使用正则表达式匹配第一个标点符号
    match = re.search(r'[，。！？；、,.!?;｜|"]', text)
    if match:
        # 找到第一个标点符号的位置并截取字符串
        return text[:match.end() - 1]
    else:
        # 如果没有找到标点符号，返回整个字符串
        return text


if __name__ == "__main__":
    root_folder_path = f"E:/03.WebArchivesInbox/inbox/{config_setting.batch_name}"
    # root_folder_path = "E:/diskstation/SingleFileWebArchives"
    for dirpath, dirnames, filenames in os.walk(root_folder_path):
        for filename in filenames:
            if len(filename) > 170:
                print(f"found overlength file {filename}")
                _, ext = os.path.splitext(filename)
                truncated_filename = truncate_text(filename)
                with codecs.open(os.path.join(dirpath, filename).replace("\\", "/"), mode='r', encoding='utf-8') as f:
                    content_head = f.read(2000)
                url_embeded = getUrl_from_content(content_head).strip()
                url_hash = hash_url(url_embeded)
                new_filename = f"{truncated_filename}__{url_hash}{ext}"
                os.rename(os.path.join(dirpath, filename), os.path.join(dirpath, new_filename))
                print(f"renamed to {new_filename}")
    print("done")


